package com.clustertest.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

@RestController

public class RedisController {
    @Autowired
    RedisTemplate redisTemplate;

    /**
     *
     * @param key 设置的key
     * @param value 设置的value
     * @param expireTime 设置的超时时间（秒）
     * @return
     */
    @GetMapping("/test/{key}/{value}/{expireTime}")
    public String function(@PathVariable String key, @PathVariable String value, @PathVariable long expireTime){
        boolean r =redisTemplate.opsForValue().setIfAbsent(key,value,expireTime, TimeUnit.SECONDS);
        System.out.println(r);
        return r?"ok":value;
    }
}
